<?php

//商品模型
class GoodsModel extends Model{
	public $table='goods';
	
	//自动验证
	public $validate=array(
       array('gname','nonull','商品名称不能为空',2,3),
       array('unit','nonull','单位不能为空',2,3),
       array('shopprice','nonull','商城价不能为空',2,3),
       
	);
	
	//添加
	public function goodsadd(){
		if(!$this->create()){
			return false;
		}
		if(IS_POST){
			$goods=Q('post.');
			$tid=K('Category')->where("cid={$goods['cid']}")->field('tid')->find();
			
			//重组商品表数组
			$str=array(
			   'gname' => $goods['gname'],
			   'unit' => $goods['unit'],
			   'marketprice' => $goods['marketprice'],
			   'shopprice' => $goods['shopprice'],
			   'pic' => $goods['thumb_list'],
			   'click' => $goods['click'],
			   'time' => time(),
			   'aid' => $_SESSION['aid'],
			   'bid' => $goods['bid'],
			   'cid' => $goods['cid'],
			   'tid' => $tid['tid'],
			);
			$gid=$this->add($str);
			
			//重组商品属性表数组
			$attr=Q('post.attr');
			foreach ($attr as $k => $v){
				 if($v){
				 	$goods_attr=array(
					     'gtvalue' => $v,
					     'taid' => $k,
					     'gid' => $gid,
					 );
					 K('Goodsattr')->add($goods_attr);
				 }
			}
			
			$str=Q('post.');
			$spec=$str['spec'];

			foreach ($spec as $key => $value) {
				foreach ($value['sp'] as $k => $v) {
					   $data = array(
						    'gtvalue'=>$v,
						     'added'=>$value['added'][$k],
						     'taid'=>$key,
						     'gid' => $gid,
						);
						K('Goodsattr')->add($data);
				}
			}
			
			 //商品详情表添加
	        $data=Q('post.');
			if (isset($data['thumb'])) {
			    $thumb = array();
				foreach ($data['thumb'] as $v) {
					if (strstr($v,'max')) {
					    $thumb ['big'][] = $v;
					}
					if (strstr($v,'medil')) {
					    $thumb['medil'][]=$v;
					}
					if (strstr($v,'small')) {
					    $thumb['small'][]=$v;
					}
				}
				//将图片地址转成字符串
				$detailArr = array(
						'small' => implode(',', $thumb['small']),
						'medium' => implode(',', $thumb['medil']),
						'big' => implode(',', $thumb['big'])
					);
				$detailArr['intro'] = $data['content'];
				$detailArr['gid'] = $gid;
				K('Detail')->add($detailArr);
			}
			
		}

		return true;
	}
	
	
	//修改
	public function goodsedit($gid){
		if(!$this->create()){
			return false;
		}
		if(IS_POST){
			$goodData=Q('post.');
			$goodData['gid']=$gid;
			$goods=$goodData;
			
			$tid=K('Category')->where("cid={$goods['cid']}")->field('tid')->find();
			//重组商品表数组
			if(isset($goods['thumb_list'])){
				$str=array(
				   'gid' => $goods['gid'],
				   'gname' => $goods['gname'],
				   'unit' => $goods['unit'],
				   'marketprice' => $goods['marketprice'],
				   'shopprice' => $goods['shopprice'],
				   'pic' => $goods['thumb_list'],
				   'click' => $goods['click'],
				   'time' => time(),
				   'aid' => $_SESSION['aid'],
				   'bid' => $goods['bid'],
				   'cid' => $goods['cid'],
				   'tid' => $tid['tid'],
			    );
				$this->update($str);
			}else{
				$str=array(
				   'gid' => $goods['gid'],
				   'gname' => $goods['gname'],
				   'unit' => $goods['unit'],
				   'marketprice' => $goods['marketprice'],
				   'shopprice' => $goods['shopprice'],
				   'pic' => $goods['pic'],
				   'click' => $goods['click'],
				   'time' => time(),
				   'aid' => $_SESSION['aid'],
				   'bid' => $goods['bid'],
				   'cid' => $goods['cid'],
				   'tid' => $tid['tid'],
			    );
				$this->update($str);
			}
			
			p($goods);
			//重组商品属性表数组
			if(isset($goods['attr'])){
				K('Goodsattr')->where("gid={$gid} AND added=0")->delete();
				$attr=$goods['attr'];
				foreach ($attr as $k => $v){
					if($v){
					 	$goods_attr=array(
						     'gtvalue' => $v,
						     'taid' => $k,
						     'gid' => $gid,
						 );
						 K('Goodsattr')->add($goods_attr);
					 }
				}
			}
			
			if(isset($goods['spec'])){
				K('Goodsattr')->where("gid={$gid} AND added!=0")->delete();
				$spec=$goods['spec'];
				foreach ($spec as $key => $value) {
					foreach ($value['sp'] as $k => $v) {
					   $data = array(
						    'gtvalue'=>$v,
						     'added'=>$value['added'][$k],
						     'taid'=>$key,
						     'gid' => $gid,
						);
						K('Goodsattr')->add($data);
					}
				}
			}
			
			 //商品详情表修改
			 if(isset($goods['thumb'])){
				 	$data=$goods;
				  
				    K('Detail')->where("gid={$gid}")->delete();
				    
					    $thumb = array();
						foreach ($data['thumb'] as $v) {
							if (strstr($v,'max')) {
							    $thumb ['big'][] = $v;
							}
							if (strstr($v,'medil')) {
							    $thumb['medil'][]=$v;
							}
							if (strstr($v,'small')) {
							    $thumb['small'][]=$v;
							}
						}
                       
						//将图片地址转成字符串
						$detailArr = array(
								'small' => implode(',', $thumb['small']),
								'medium' => implode(',', $thumb['medil']),
								'big' => implode(',', $thumb['big']),
							);
						$detailArr['intro'] = $data['content'];
						$detailArr['gid'] = $gid;
						
						K('Detail')->add($detailArr);
					
			 }else{
			 	    $data=$goods;
					K('Detail')->where("gid={$gid}")->delete();
			 	    $detailArr = array(
			 	            'gid' =>$gid,
							'small' => $data['small'],
							'medium' => $data['medil'],
							'big' => $data['big'],
							'intro' =>$data['content'],
							
						);
					K('Detail')->add($detailArr);
			 }
	        
			
			
		}
		  

		  return true;
	}
	
	
	
	
}

?>